package gui.dialogs;

import gui.components.Dialogable;
import gui.components.MImage;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;

import core.Configuration;

public class ListDialog extends GameDialog{
	
	private static int DIALOG_WIDTH = 450;
	private static int DIALOG_HEIGHT = 150;
	private static int LINE_HEIGHT = 35;

	private Window parent;
	
	static{
		DIALOG_WIDTH = (int)(DIALOG_WIDTH*Configuration.zoomConst);
		DIALOG_HEIGHT = (int)(DIALOG_HEIGHT*Configuration.zoomConst);
	}
		
	public ListDialog(Window parent, List<Dialogable> list, String title, String buttonText, ActionListener al){
		super(parent);
		
		DIALOG_HEIGHT += list.size()*LINE_HEIGHT;
		
		this.setSize(DIALOG_WIDTH,DIALOG_HEIGHT);
		
		this.parent = parent;
		Container c = this.getContentPane();		
		
		MImage bkg = new MImage(Configuration.IMAGE_FOLDER_ILLUSTRATION+"gameBoardTransp.jpg");
		bkg.setBounds((int)(-(bkg.getSize().width-DIALOG_WIDTH)/2), (int)(-(bkg.getSize().height-DIALOG_HEIGHT)/2), bkg.getSize().width, bkg.getSize().height);
		
		JLabel title1 = new JLabel(title);
		title1.setForeground(new Color(228,42,42));
		title1.setFont(new Font("Sans Serif",Font.BOLD,18));
		title1.setBounds(0,10, DIALOG_WIDTH, 35);
		title1.setHorizontalAlignment(JLabel.CENTER);
			
		int i = 0;
		int startPos = 70;
		for(Dialogable d:list){
			JLabel itemTitle = new JLabel(d.getName());
			itemTitle.setForeground(Color.BLACK);
			itemTitle.setFont(new Font("Sans Serif",Font.BOLD,14));
			itemTitle.setBounds(20,startPos+i*LINE_HEIGHT, DIALOG_WIDTH, LINE_HEIGHT);
			itemTitle.setHorizontalAlignment(JLabel.LEFT);	
			
			JButton itemButton = new JButton(buttonText);
			
			itemButton.setBounds(DIALOG_WIDTH-100-20,startPos+i*LINE_HEIGHT+5, 100, LINE_HEIGHT-10);				
			itemButton.addActionListener(al);
			
			c.add(itemButton);
			c.add(itemTitle);
			i++;
		}		
		
		JButton closeButton = new JButton("Close");
		closeButton.setBounds((DIALOG_WIDTH-100)/2,DIALOG_HEIGHT-LINE_HEIGHT-20, 100, LINE_HEIGHT-10);				
		closeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
			    getInstance().getParentWindow().setEnabled(true);
				getInstance().dispose();				
			}
		});

		c.add(title1);
		c.add(closeButton);
		c.add(bkg);
		
		this.setVisible(true);			
	}

}

